<?php

namespace App\Admin\Controllers;

use App\Admin\Grid\NGrid;
use App\Models\OrderModel;
use Encore\Admin\Show;

class OrderController extends BaseController
{
    protected $title = '短信管理';

    /**
     * grid
     * @return NGrid
     */
    protected function grid(): NGrid
    {
        $grid = new NGrid(new OrderModel);
        $grid->model()->leftJoin('order_system', 'order_system.order_id', 'order.id')
            ->leftJoin('order_status', 'order_status.order_id', 'order.id')
            ->leftJoin('seller', 'seller.user_id', 'order.seller_id')
            ->leftJoin('seller_dm', 'seller_dm.user_id', 'seller.user_id')
            ->leftJoin('user as seller_user', 'seller_user.id', 'seller_dm.user_id')
            ->leftJoin('user', 'user.id', 'order.user_id')
            ->leftJoin('user_dm', 'user_dm.user_id', 'user.id')
            ->select([
                'order_status.*',
                'order.*',
                'order_system.id as system',
                'user.mobile as user_mobile',
                'seller_user.mobile as seller_mobile',
                'user_dm.name as user_name',
                'user_dm.nickname',
                'seller.name as seller_name',
                'seller_dm.real_name',
                'order_system.id as system'
            ])
            ->orderByDesc('order.id');
        //$grid->expandFilter();
        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('order-id', __('ID'))->integer();
                $filter->equal('order_status-status', __('order.status'))->select(__('order.statusArr'));
                $filter->equal('title', __('order.title'));
                $filter->equal('seller-name', __('com.seller_name'));
                $filter->equal('user-mobile', __('com.user_mobile'))->integer();
                $filter->equal('user_dm-name', __('com.user_name'));
                $filter->where(function ($query) {
                    if ((int)$this->input === 0) $query->where('main_id', '>', 0);
                    else $query->where('main_id', 0);
                }, __('order.main_id'))->select(__('com.whether'));
                $filter->where(function ($query) {
                    $query->where('user.created_at', '>=', $this->input);
                }, __('com.created_at') . '>=')->datetime();
            });
            $filter->column(1 / 2, function ($filter) {
                $filter->equal('order-serial_number', __('com.serial_number'));
                $filter->where(function ($query) {
                    if ((int)$this->input === 0) $query->whereNull('order_system.id');
                    else $query->whereNotNull('order_system.id');
                }, __('order.system'))->select(__('com.whether'));
                $filter->equal('seller_user-mobile', __('com.seller_mobile'))->integer();
                $filter->equal('real_name', __('com.real_name'));
                $filter->equal('user_dm-nickname', __('com.nickname'));
                $filter->equal('start_at', __('order.start_at'))->datetime();
                $filter->where(function ($query) {
                    if ((int)$this->input === 0) $query->where('parent_id', 0);
                    else $query->where('parent_id', '>', 0);
                }, __('order.parent_id'))->select(__('com.whether'));
                $filter->where(function ($query) {
                    $query->where('user.created_at', '<=', $this->input);
                }, __('com.created_at') . '<=')->datetime();
            });
        });
        $grid->column('id', __('ID'))->sortable();
        $grid->column('serial_number', __('com.serial_number'));
        $grid->column('status', __('order.status'))->using(__('order.statusArr'))->sortable();
        $grid->column('system', __('order.system'))->display(function ($field) {
            return is_null($field) ? '否' : '是';
        });
        $grid->column('main_id', __('order.main_id'))->display(function ($field) {
            return $field === 0 ? '是' : '否';
        });
        $grid->column('parent_id', __('order.parent_id'))->display(function ($field) {
            return $field > 0 ? '是' : '否';
        });
        $grid->column('title', __('order.title'));
        $grid->column('duration', __('order.duration'));
        $grid->column('seller_mobile', __('com.seller_mobile'));
        $grid->column('seller_name', __('com.seller_name'));
        $grid->column('real_name', __('com.real_name'));
        $grid->column('user_mobile', __('com.user_mobile'));
        $grid->column('nickname', __('com.nickname'));
        $grid->column('name', __('com.user_name'));
        $grid->column('start_at', __('order.start_at'));
        $grid->column('created_at', __('com.created_at'));
        return $grid;
    }

    /**
     * detail
     * @param int $id
     * @return Show
     */
    protected function detail(int $id): Show
    {
        $show = new Show(OrderModel::query()->leftJoin('order_system', 'order_system.order_id', 'order.id')
            ->leftJoin('order_status', 'order_status.order_id', 'order.id')
            ->leftJoin('seller', 'seller.user_id', 'order.seller_id')
            ->leftJoin('seller_dm', 'seller_dm.user_id', 'seller.user_id')
            ->leftJoin('user as seller_user', 'seller_user.id', 'seller_dm.user_id')
            ->leftJoin('user', 'user.id', 'order.user_id')
            ->leftJoin('store', 'store.id', 'order.store_id')
            ->leftJoin('user_dm', 'user_dm.user_id', 'user.id')->where('order.id', $id)
            ->first([
                'order_status.*',
                'order.*',
                'order_system.id as system',
                'user.mobile as user_mobile',
                'seller_user.mobile as seller_mobile',
                'user_dm.name as user_name',
                'user_dm.nickname',
                'seller.name as seller_name',
                'seller_dm.real_name',
                'order_system.id as system',
                'store.name as store_name',
            ]));
        $show->field('id', __('ID'));
        $show->field('serial_number', __('com.serial_number'));
        $show->field('status', __('order.status'))->using(__('order.statusArr'));
        $show->field('system', __('order.system'))->as(function ($field) {
            return is_null($field) ? '否' : '是';
        });
        $show->field('title', __('order.title'));
        $show->field('duration', __('order.duration'));
        $show->field('seller_mobile', __('com.seller_mobile'));
        $show->field('seller_name', __('com.seller_name'));
        $show->field('real_name', __('com.real_name'));
        $show->field('user_mobile', __('com.user_mobile'));
        $show->field('nickname', __('com.nickname'));
        $show->field('name', __('com.user_name'));
        $show->field('store_name', __('com.store_name'));
        $show->field('address_id', __('com.address_id'));
        $show->field('main_id', __('order.main_id'))->as(function ($field) {
            return $field === 0 ? '是' : '否';
        });
        $show->field('parent_id', __('order.parent_id'))->as(function ($field) {
            return $field > 0 ? '是' : '否';
        });
        $show->field('coupon_user_related_id', __('order.coupon_user_related_id'))->as(function ($field) {
            return $field > 0 ? '是' : '否';
        });
        $show->field('sign_file_id', __('order.sign_file_id'))->as(function ($field) {
            return fileIdToUrl($field);
        })->image(config('oss.url'), 100);
        $show->field('fill_count', __('order.fill_count'));
        $show->field('fill_price', __('order.fill_price'));
        $show->field('start_at', __('order.start_at'));
        $show->field('created_at', __('com.created_at'));
        return $show;
    }
}
